//
//  BookStoreTableViewController.swift
//  BookStore2.0
//
//  Created by hw on 2022/5/31.
//

import UIKit

class BookStoreTableViewController: UITableViewController {
    
    static var ClickedID:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let xib=UINib(nibName: "BookStoreTableViewCell", bundle: nil)
        tableView.register(xib, forCellReuseIdentifier: "bookcell")
        tableView.rowHeight=130
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return DB1.kinds.count
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return DB1.kinds[section]
    }
    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return DB1.kinds;
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        let num=DB1.kinds[section]
        let section = DB1.BookBasicInfoDictionary[num]!
        return section.count
        
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "bookcell", for: indexPath) as! BookStoreTableViewCell
        let kind=DB1.kinds[indexPath.section]
        let mySection=DB1.BookBasicInfoDictionary[kind]!
        let id=mySection[indexPath.row]["id"] as! String
        let imgUrl=mySection[indexPath.row]["imgUrl"] as! String
        let imgResult:UIImage?=DB1.imgSet[id]
        if imgResult==nil{
            cell.img1.downloadpicAsync(url: imgUrl,bookid: id)
        }
        else
        {
            cell.img1.image=imgResult
        }
        let price=(mySection[indexPath.row]["price"] as! NSNumber).floatValue
        cell.price1.text="RMB:\(price)"
        cell.title1.text=mySection[indexPath.row]["title"] as? String
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let kind=DB1.kinds[indexPath.section]
        let mySection=DB1.BookBasicInfoDictionary[kind]!
        BookStoreTableViewController.ClickedID=mySection[indexPath.row]["id"] as? String
        performSegue(withIdentifier: "getDetail", sender: self)
       }
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
        let detailVC=segue.destination as! BookDetailViewController
        detailVC.ID=BookStoreTableViewController.ClickedID
    }
}
extension UIImageView{
    func downloadpicAsync(url:String,bookid:String)
    {
        let urlNet=URL(string: url)
        let task=URLSession.shared.dataTask(with: urlNet!)
        {
            (data,response,error) in
            if let nsd=data{
                DispatchQueue.main.async {
                    self.image=UIImage(data: nsd,scale: 1)
                    self.contentMode = .scaleAspectFit
                }
            }
        }
        task.resume()
    }
}
